package com.cloudcc.mobile.view.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.GroupInfoAdapter;
import com.cloudcc.mobile.adapter.GroupInfoAdapter.OnItemListener;
import com.cloudcc.mobile.entity.group.GetGroupInfo;
import com.cloudcc.mobile.entity.group.GetGroupInfoMessage;
import com.cloudcc.mobile.entity.group.GroupInfo;
import com.cloudcc.mobile.parser.GetGroupInfoParser;
import com.cloudcc.mobile.service.GetGroupInfoService;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.weight.CustomListView;
import com.cloudcc.mobile.weight.CustomListView.OnRefreshListener;
import com.cloudcc.mobile.weight.LoadingView;
/** * 显示分组界面 * @author Administrator * */
public class GroupActivity extends BaseActivity{
	private ImageView  fanhui;
	private CustomListView listview;
	private LoadingView loadingview;
	private GetGroupInfo info;
	private LinearLayout group_network;
	int page=1;
	String group_name="";
	private Context context;
	private GetGroupInfoService service;
	List<GroupInfo> total=new ArrayList<GroupInfo>();
	private GroupInfoAdapter adapter;
	private GroupInfo first,last;
	int pp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		context=this;
		seupView();
		addListener();
	}

	@Override
	public int getLayoutId() {
		return R.layout.aboutgroup;
	}
	private void seupView() {
		adapter=new GroupInfoAdapter(context);
		first=new GroupInfo();
		first.setName("全公司");
		first.setId("");
		last=new GroupInfo();
		last.setName("我自己");
		last.setId("");
		group_name=getIntent().getStringExtra("quanxian");
		fanhui=(ImageView)findViewById(R.id.fanhuiimg);
		listview=(CustomListView)findViewById(R.id.group);
		loadingview=(LoadingView)findViewById(R.id.grouploading);
		group_network=(LinearLayout)findViewById(R.id.group_nonetWork);
		service=new GetGroupInfoService();
		if(loadingview.getVisibility()==View.VISIBLE){
			listview.setCanRefresh(false);
		}
		OnRefresh();
	}
	
	private void OnRefresh() {
		info=new GetGroupInfo();
		info.setLimit(0);
		info.setPageNUM(0);
		info.setQueryType("my");
		info.setSortValue(-1);
		String data=Json.toJson(info);
		new getGroupInfos().execute(data);
	}

	private void addListener() {
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				GroupActivity.this.finish();
			}
		});
		group_network.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			loadingview.setVisibility(View.VISIBLE);
			group_network.setVisibility(View.GONE);
			OnRefresh();
		}});
		listview.setOnRefreshListener(new OnRefreshListener() {@Override public void onRefresh() {
			OnRefresh();

		}});

		adapter.setOnItemtListener(new OnItemListener() {
			@Override
			public void zhengti(int position) {
				total.get(position).setSelect(true);
				total.get(pp).setSelect(false);
				adapter.changeData(total);
				Intent intent=new Intent();
				intent.putExtra("groupname", total.get(position).getName());
				intent.putExtra("groupid", total.get(position).getId());
				setResult(RESULT_OK, intent);
				GroupActivity.this.finish();
			}
		});
	}
	Handler hanlder=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case -1:
				Tools.showInfo(context, "获取组信息失败");
				loadingview.setVisibility(View.GONE);
				group_network.setVisibility(View.VISIBLE);
				listview.setVisibility(View.GONE);
				break;
			case 1:
				loadingview.setVisibility(View.GONE);
				group_network.setVisibility(View.GONE);
				adapter.changeData(total);
				listview.setAdapter(adapter);
				listview.setClickable(false);
				listview.setSelected(false);
				listview.onRefreshComplete();
				break;
			}
			super.handleMessage(msg);
		}
	};
	class getGroupInfos extends AsyncTask<String, Void, String>{
		@Override
		protected String doInBackground(String... params) {
			String data=params[0];
			String result=service.getgroupinfo(data);
			return result;
		}
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			try {
				GetGroupInfoMessage msg=GetGroupInfoParser.getGroupInfo(result);
				if(msg.isResult()){
					total.clear();
					total.add(0, first);
					total.addAll(msg.getList());
//					total.add(last);
					if(!group_name.equals("")){
						for(int i=0;i<total.size();i++){
							if(group_name.equals(total.get(i).getName())){
								total.get(i).setSelect(true);
								pp=i;
							}
						}
					}
					hanlder.sendEmptyMessage(1);
				}else{
					hanlder.sendEmptyMessage(-1);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}
}
